﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace EasyFlow
{
    /// <summary>
    /// VariableTreeView.xaml 的交互逻辑
    /// </summary>
    public partial class VariableTreeView : Window
    {
        private ICollectionView collectionView;
        public ICommand VariableSearchButtonClickCommand
        {
            get { return (ICommand)GetValue(VariableSearchButtonClickCommandProperty); }
            set { SetValue(VariableSearchButtonClickCommandProperty, value); }
        }
        public static readonly DependencyProperty VariableSearchButtonClickCommandProperty =
            DependencyProperty.Register("VariableSearchButtonClickCommand", typeof(ICommand), typeof(VariableTreeView), new PropertyMetadata(null));
        public ICommand VariableSelectionChangedCommand
        {
            get { return (ICommand)GetValue(VariableSelectionChangedCommandProperty); }
            set { SetValue(VariableSelectionChangedCommandProperty, value); }
        }
        public static readonly DependencyProperty VariableSelectionChangedCommandProperty =
            DependencyProperty.Register("VariableSelectionChangedCommand", typeof(ICommand), typeof(VariableTreeView), new PropertyMetadata(null));
        public ICommand ReplaceVariableCommand
        {
            get { return (ICommand)GetValue(ReplaceVariableCommandProperty); }
            set { SetValue(ReplaceVariableCommandProperty, value); }
        }
        public static readonly DependencyProperty ReplaceVariableCommandProperty =
            DependencyProperty.Register("ReplaceVariableCommand", typeof(ICommand), typeof(VariableTreeView), new PropertyMetadata(null));
        public ICommand AppendVariableCommand
        {
            get { return (ICommand)GetValue(AppendVariableCommandProperty); }
            set { SetValue(AppendVariableCommandProperty, value); }
        }
        public static readonly DependencyProperty AppendVariableCommandProperty =
            DependencyProperty.Register("AppendVariableCommand", typeof(ICommand), typeof(VariableTreeView), new PropertyMetadata(null));
        public string SelectedVariableName
        {
            get { return (string)GetValue(SelectedVariableNameProperty); }
            set { SetValue(SelectedVariableNameProperty, value); }
        }
        public static readonly DependencyProperty SelectedVariableNameProperty =
            DependencyProperty.Register("SelectedVariableName", typeof(string), typeof(VariableTreeView), new PropertyMetadata(""));

        public VariableTreeView()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            VariableViewModel vvm = (sender as Button).DataContext as VariableViewModel;
            vvm.Value = vvm.BindingVariable.ToString();
        }

        private void SearchButton_Click(object sender, RoutedEventArgs e)
        {
            VariableSearchButtonClickCommand?.Execute(SearchTxt.Text);
        }

        VariableViewModel vvm;
        private void VariableTree_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            vvm = e.NewValue as VariableViewModel;
            if (vvm != null && !vvm.IsNodeOnly)
            {
                VariableSelectionChangedCommand?.Execute(e.NewValue);
                SelectedVariableName = (e.NewValue as VariableViewModel).Name;
            }
            e.Handled = true;
        }

        private void ReplaceButton_Click(object sender, RoutedEventArgs e)
        {
            if (vvm == null)
            {
                MessageBox.Show("请选择变量!");
                return;
            }
            ReplaceVariableCommand?.Execute(vvm);
        }

        private void AppendButton_Click(object sender, RoutedEventArgs e)
        {
            if (vvm == null)
            {
                MessageBox.Show("请选择变量!");
                return;
            }
            AppendVariableCommand?.Execute(vvm);
        }
    }
}
